package com.war.service.system;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.war.dao.system.WarehouseDao;
import com.war.pojo.stock.TransferInfo;
import com.war.pojo.system.User;
import com.war.pojo.system.Warehouse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class WarehouseServiceImpl implements WarehouseService{
    @Autowired(required = false)
    WarehouseDao warehouseDao;

    @Override
    public HashMap<String, Object> selectAll() {
        HashMap<String, Object> map = new HashMap<String, Object>();
        List<Warehouse> list = warehouseDao.selectAll();
        map.put("list",list);
        return map;
    }

    @Override
    public List<Warehouse> selectName() {
        return warehouseDao.selectName();
    }

    @Override
    public HashMap<String, Object> select(Warehouse warehouse) {
        HashMap<String, Object> map =  new HashMap<String, Object>();
        //1 设置分页参数
        PageHelper.startPage(warehouse.getPage(),warehouse.getRow());
        //根据用户选中的查询条件判断用户需要查询的
        List<Warehouse> list = new ArrayList<>();
        if(warehouse.getConValue().equals("")){
            list = warehouseDao.selectAll();
        }else {
            if(warehouse.getCondition().equals("仓库名称")){
                //设置查询条件
                warehouse.setName(warehouse.getConValue());
                list.add( warehouseDao.selectByName(warehouse));
            }else if(warehouse.getCondition().equals("存放商品类型")){
                warehouse.setType(warehouse.getConValue());
                list.add( warehouseDao.selectByType(warehouse));
            }else {
                list = warehouseDao.selectAll();
            }
        }
        //3.把查询的数据转换成分页对象
        PageInfo<Warehouse> page = new PageInfo<Warehouse>(list);

        //获取分页的当前页的集合
        map.put("list",page.getList());
        //总条数
        map.put("total",page.getTotal());
        //总页数
        map.put("totalPage",page.getPages());
        //上一页
        if(page.getPrePage()==0){
            map.put("pre",1);
        }else{
            map.put("pre",page.getPrePage());
        }

        //下一页
        //保持在最后一页
        if(page.getNextPage()==0){
            map.put("next",page.getPages());
        }else{
            map.put("next",page.getNextPage());
        }
        //当前页
        map.put("cur",page.getPageNum());

        return map;
    }

    @Override
    public Warehouse selectByid(Warehouse warehouse) {
        return warehouseDao.selectById(warehouse);
    }

    @Override
    public String delWare(Warehouse warehouse){
        int num = warehouseDao.delWare(warehouse);
        if(num>0){
            return "删除成功";
        }
        return "删除失败";
    }

    @Override
    public String addWare(Warehouse warehouse){
        Warehouse w = warehouseDao.selectByName(warehouse);
        if(w != null && w.getId() != warehouse.getId() ){
            return "当前仓库名已被注册";
        }else {
            int n = warehouseDao.addWare(warehouse);
            if(n>0){
                return "添加成功";
            }
            return "添加失败";
        }
    }

    @Override
    public String editIWare(Warehouse warehouse) {
        Warehouse w = warehouseDao.selectByName(warehouse);
        if(w != null && w.getId() != warehouse.getId() ){
            return "当前仓库名已被注册";
        }else {
            int n = warehouseDao.updateWare(warehouse);
            if(n>0){
                return "修改成功";
            }
            return "修改失败";
        }
    }

}
